<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>E键配</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">

    <link href="./static/plugins/bootstrap/bootstrap.min.css" rel="stylesheet">
    <link href="./static/css/font-awesome.min.css" rel="stylesheet">
    <link href="./static/plugins/toastr/toastr.min.css" rel="stylesheet">
    <link href="./static/css/animate.css" rel="stylesheet">
    <link href="./static/css/style.css" rel="stylesheet">
    <link href="./static/plugins/ladda/ladda-themeless.min.css" rel="stylesheet">

    <script type="text/javascript" src="./static/js/jquery-3.1.1.min.js"></script>
    <script type="text/javascript" href="./static/plugins/bootstrap/bootstrap.min.js"></script>
    <script type="text/javascript" src="./static/plugins/toastr/toastr.min.js"></script>
    <script type="text/javascript" src="./static/plugins/validate/jquery.validate.min.js"></script>
    <script type="text/javascript" src="./static/plugins/ladda/spin.min.js"></script>
    <script type="text/javascript" src="./static/plugins/ladda/ladda.min.js"></script>
    <script type="text/javascript" src="./static/plugins/ladda/ladda.jquery.min.js"></script>
    <script type="text/javascript" src="./static/js/main.js"></script>
    <script>
        if (token && token.length > 30) {
            location.href = '/container.html';
        }
    </script>
</head>
<body class="gray-bg">
<div class="middle-box text-center loginscreen animated fadeInDown">
    <div>
        <div>
            <img class="logo-name"
                 src=""
                 alt="E键配">
        </div>
        <!--<h3>E键配</h3>-->
        <p class="m-t">郑州畅配网络科技有限公司</p>
        <form class="m-t m-b" role="form" id="theForm">
            <div class="form-group">
                <div class="input-group">
                    <input type="text" name="phone" class="form-control" placeholder="请输入手机号">
                    <span class="input-group-btn">
                            <button type="button" id="verify-btn" class="ladda-button btn btn-disabled" data-style="expand-right"><small>发送验证码</small></button>
                        </span>
                </div>
            </div>
            <div class="form-group">
                <input type="text" name="smscode" class="form-control" placeholder="请输入短信验证码" maxlength="4">
            </div>
            <div class="form-group">
                <input type="password" name="password" id="password" class="form-control" placeholder="请输入登录密码">
            </div>
            <div class="form-group">
                <input type="password" name="repassword" class="form-control" placeholder="请再次输入登录密码">
            </div>
            <input type="hidden" name="from" value="pc"/>
            <button type="submit" class="ladda-button btn btn-primary btn-block m-b" data-style="expand-right" id="sub-btn">确定重置</button>
        </form>
        <a href="index.html">
            <small>去登录</small>
        </a>
    </div>
</div>

<script>
    $(function () {
        $('#theForm').validate({
            rules: {
                smscode: {
                    required: true
                },
                password: {
                    required: true,
                },
                repassword: {
                    equalTo: '#password',
                }
            },
            messages: {
                smscode: {
                    required: '<i class="icon icon-exclamation-sign"></i>短信验证码不能为空'
                },
                password: {
                    required: '<i class="icon icon-exclamation-sign"></i>密码不能为空',
                },
                repassword: {
                    equalTo: '<i class="icon icon-exclamation-sign"></i>两次输入的密码不一致',
                }
            },
            submitHandler: function (form) {
                var btn = $('#sub-btn').ladda().ladda('start');
                $.ajax({
                    type: 'POST',
                    url: api + "index/resetPasswd",
                    data: $(form).serialize(),
                    dataType: 'json',
                    success: function (data) {
                        if (data.ret == 0) {
                            toastr.success(data.msg, null, {
                                timeOut: 1500,
                                onHidden: function () {
                                    btn.ladda('stop');
                                    location.href = 'index.html';
                                }
                            });
                        } else {
                            btn.ladda('stop');
                            toastr.error(data.msg);
                        }
                    },
                    error: function (xhr, type) {
                        btn.ladda('stop');
                        toastr.error(xhr.responseJSON.msg);
                    }
                });
            }
        });

        $('input[name="phone"]').on('keyup', function () {
            if (checkMobile(this.value)) {
                $('#verify-btn').removeClass('btn-disabled').addClass('btn-primary');
            } else {
                $('#verify-btn').removeClass('btn-primary').addClass('btn-disabled');
            }
        });

        $('#verify-btn').on('click', function () {
            var btn = $(this);
            if (btn.hasClass('btn-disabled') || btn.hasClass('pendding')) {
                toastr.warning('手机号码不正确');
                return;
            }
            var phone = $('input[name="phone"]').val();
            btn.ladda().ladda('start');
            $.ajax({
                type: 'POST',
                url: api + 'index/sendSms',
                data: {phone: phone},
                success: function (data) {
                    btn.ladda('stop');
                    counter();
                }
            });
        });
    });
    var handle;

    function counter() {
        var times = 30;
        var btn = $('#verify-btn');
        btn.removeClass('btn-primary').addClass('pendding').find('small').text('已发送');
        handle = setInterval(function () {
            if (times == 0) {
                btn.removeClass('pendding').addClass('btn-primary').find('small').text('重新发送');
                clearInterval(handle);
            } else {
                times--;
                btn.find('small').text('已发送(' + times + ')');
            }
        }, 1000);
    }
</script>
</body>
</html>
